package com.itheima.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class AliUploadUtil {
    private static  String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    private static  String accessKeyId = "LTAI4FdML4iYudjzNFMVLiCh";
    private static  String accessKeySecret = "RE3C3Eh2ZM762EObJWbOKTjJJbc964";
    private static String  bucketName ="health-program";


    public static  void uploadFile(byte[] bys, String fileName) throws  Exception{
// Endpoint以杭州为例，其它Region请按实际情况填写。

// 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);

// 上传Byte数组。
        byte[] content = bys;
        ossClient.putObject(bucketName, fileName, new ByteArrayInputStream(content));

// 关闭OSSClient。
        ossClient.shutdown();
    }

    public static void deleteFile(List<String> fileNames){
//        String bucketName = "<yourBucketName>";

// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// 删除文件。key等同于ObjectName，表示删除OSS文件时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg。
        List<String> keys = new ArrayList<String>();
        keys.add("key0");
        keys.add("key1");
        keys.add("key2");

        DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys));
        List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();

// 关闭OSSClient。
        ossClient.shutdown();
    }

}
